Javascript replace space with regex


\s : means "one space"

\s+ : means "one or more spaces"

let stringWithSpace = '  A B  C   D EF ';

console.log(str.replace(/\s/g, '#'));                                
// ##A#B##C###D#EF# => means one space replace by one #;

console.log(str.replace(/\s+/g, '#'));                             
// #A#B#C#D#EF#  => means one or more spaces, all replace by #;
#javascript #replace #RegEx






你可能感興趣的文章

JS註冊組|瀏覽器點擊上/下一頁時能重新刷新頁面

JS註冊組|瀏覽器點擊上/下一頁時能重新刷新頁面

ASP.NET Core Web API 入門教學 - 關鍵字搜尋

ASP.NET Core Web API 入門教學 - 關鍵字搜尋

Navigating Rhode Island's Window Tint Laws: A Comprehensive Overview

Navigating Rhode Island's Window Tint Laws: A Comprehensive Overview






留言討論